package com.fx.controller;

import com.fx.common.AbstractWindowController;
import com.fx.common.impl.ApplicationControlActionController;
import com.fx.common.impl.CreateProjectController;
import com.fx.common.impl.JFXButtonActionHandler;
import com.fx.gen.domain.BuildProjectInfo;
import com.fx.server.FxUiNewServer;
import com.fx.view.CreateNewProjectView;
import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.RadioButton;
import javafx.scene.control.Toggle;
import javafx.scene.control.ToggleGroup;
import javafx.stage.WindowEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URL;
import java.util.ResourceBundle;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@FXMLController
public class CreateNewProjectController extends AbstractWindowController implements Initializable {

    @Autowired
    private CreateNewProjectView createNewProjectView;

    @Autowired
    private JFXButtonActionHandler jfxButtonActionHandler;

    @Autowired
    private FxUiNewServer fxUiNewServer;

    @FXML
    private ToggleGroup group;

    @FXML
    private RadioButton groupJar;

    @FXML
    private RadioButton groupPom;

    @FXML
    private JFXButton nextButton;

    @FXML
    private JFXButton createButton;

    @FXML
    private JFXTextField projectName;


    @Override
    protected void makeRoot() {
        setRoot(createNewProjectView.getView());
    }

    @Override
    public void onCloseRequest(WindowEvent e) {
        closeWindow();
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        groupJar.setUserData("jar");
        groupPom.setUserData("pom");
        final AtomicReference<String> packageType = new AtomicReference<>();
        group.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                if("jar".equals(newValue.getUserData().toString())){
                    nextButton.setVisible(true);
                    nextButton.setManaged(false);
                    packageType.set("jar");
                } else {
                    nextButton.setVisible(false);
                    nextButton.setManaged(true);
                    packageType.set("pom");
                }
            }
        });
        createButton.setUserData(new CreateProjectController(()-> buildProjectInfo(packageType),fxUiNewServer));
        jfxButtonActionHandler.bindAction(createButton);
    }


    private BuildProjectInfo buildProjectInfo(AtomicReference<String> packageType){
        BuildProjectInfo info = new BuildProjectInfo();
        info.setName(projectName.getText());
        return info;
    }
}
